<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>EchopenAI API 示例 - JavaScript</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
        }
        .container {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        textarea, input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        button {
            background: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            background: #e9ecef;
            border-radius: 4px;
            white-space: pre-wrap;
        }
        .error {
            color: #dc3545;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>EchopenAI API JavaScript 示例</h1>
        
        <div>
            <label for="apiKey">API 密钥:</label>
            <input type="text" id="apiKey" placeholder="请输入您的API密钥">
        </div>
        
        <div>
            <label for="text">要处理的文本:</label>
            <textarea id="text" rows="5" placeholder="请输入需要处理的文本内容...">高村乡农村集体经济发展的重要因素是带头人的能力水平...</textarea>
        </div>
        
        <div>
            <label for="serviceType">服务类型:</label>
            <select id="serviceType">
                <option value="jc">降重 (jc)</option>
                <option value="aigc">降AIGC (aigc)</option>
                <option value="complex">复合服务 (complex)</option>
            </select>
        </div>
        
        <button onclick="callAPI()">调用API</button>
        
        <div id="result" class="result" style="display:none;"></div>
    </div>

    <script>
        async function callAPI() {
            const apiKey = document.getElementById('apiKey').value.trim();
            const text = document.getElementById('text').value.trim();
            const serviceType = document.getElementById('serviceType').value;
            const resultDiv = document.getElementById('result');
            
            if (!apiKey) {
                showError('请输入API密钥');
                return;
            }
            
            if (!text) {
                showError('请输入要处理的文本');
                return;
            }
            
            try {
                // 显示加载状态
                resultDiv.style.display = 'block';
                resultDiv.className = 'result';
                resultDiv.innerHTML = '正在处理...';
                
                const response = await fetch('https://api.llmapi.fit/completion/reduce', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer ${apiKey}`
                    },
                    body: JSON.stringify({
                        text: text,
                        service_type: serviceType
                    })
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    resultDiv.innerHTML = `
                        <strong>处理成功!</strong><br><br>
                        <strong>请求ID:</strong> ${data.request_id}<br>
                        <strong>输入字数:</strong> ${data.input_text_count}<br>
                        <strong>服务类型:</strong> ${data.service_type}<br>
                        <strong>处理结果:</strong><br>
                        <div style="margin-top: 10px; padding: 10px; background: white; border-radius: 4px;">
                            ${data.output_text}
                        </div>
                    `;
                } else {
                    showError(`错误 (${response.status}): ${data.message || '未知错误'}`);
                }
            } catch (error) {
                showError(`请求失败: ${error.message}`);
            }
        }
        
        function showError(message) {
            const resultDiv = document.getElementById('result');
            resultDiv.style.display = 'block';
            resultDiv.className = 'result error';
            resultDiv.innerHTML = `<strong>错误:</strong> ${message}`;
        }
    </script>
</body>
</html>